/**
 * Created by gitbong on 14-3-7.
 * base on jq1.6.2 gbbase
 */
var gb = gb = gb || {};
(function () {
    gb.mvc = {
        //M
        _mMap: {},
        registerM: function (ins_) {
            this._mMap[ins_.name] = ins_;
            ins_.onRegister();
        },
        removeM: function (name_) {
            var $m = this._mMap[name_];
            if ($m != null) {
                $m.onRemove();
                delete this._mMap[name_];
//        this._mMap[name_] = null;
            }
        },
        getM: function (name_) {
            return this._mMap[name_];
        },

        //V
        _vMap: {},
        registerV: function (ins_) {
            this._vMap[ins_.name] = ins_;
            ins_.onRegister();
        },
        removeV: function (name_) {
            var $v = this._vMap[name_];

            if ($v != null) {
                $v.onRemove()
                delete this._vMap[name_];
            }
        },
        getV: function (name_) {
            return this._vMap[name_];
        },

        //C
        _cMap: {},
        registerC: function (noteName_, classRef_) {
            this._cMap[noteName_] = classRef_;
        },
        removeC: function (noteName_) {
            this._cMap[noteName_] = null;
        }
    };
    function onVHandlerNote(note_) {
        var $map = gb.mvc._vMap;
        var $v;
        for (var i in $map) {
            $v = $map[i];
            if ($v != null) {
                for (var i = 0; i < $v.noteList.length; i++) {
                    var e = $v.noteList[i];
                    if (e == note_.name) {
                        $v.handleNote(note_);
                    }
                }
            }
        }
    }

    function onCHandlerNote(note_) {
        var $map = gb.mvc._cMap;
        var $class = $map[note_.name];
        if ($class != null) {
            var $c = new $class;
            $c.execute(note_);
        }
    }

    var $Notifer = gb.classes.Class.extend({
        isRegister: false,
        onRegister: function () {
            this.isRegister = true;
        },
        onRemove: function () {
            this.isRegister = false;
        },
        sendNote: function (name_, body_, type_) {
            if (this.isRegister) {
                var $note = {name: name_, body: body_, type: type_};
                onVHandlerNote($note);
                onCHandlerNote($note);
            }
        }
    });
    gb.mvc.M = $Notifer.extend({
        _mvcAttrs: {type: 'Model'},
        name: 'M',
        data: {},
        ctor: function (name_, data_) {
            this.name = name_;
            this.data = data_;
        }
    });
    gb.mvc.V = $Notifer.extend({
        _mvcAttrs: {type: 'View'},
        name: 'M',
        view: null,
        noteList: [],
        ctor: function (name_, view_) {
            this.name = name_;
            this.view = view_;
            this.noteList = [];
        },
        addNoteListener: function (name_) {
            if (this.noteList.indexOf(name_) == -1)
                this.noteList.push(name_);
        },
        removeNoteListener: function (name_) {
            var $id = this.noteList.indexOf(name_);
            if ($id != -1)
                this.noteList.splice($id, 1);
        },
        handleNote: function (note_) {

        }
    });
    gb.mvc.C = gb.classes.Class.extend({
        _mvcAttrs: {type: 'Controller'},
        execute: function (note_) {
        }
    });
})();